/*© 2010-2011 mOcean Mobile. A subsidiary of Mojiva, Inc. All Rights Reserved.*/
package com.adserver.adview;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.io.IOException;

/**
 * Viewer of interstitial advertising.
 */
public class AdServerInterstitialView extends AdServerView {
    private int autoCloseInterstitialTime;

    // Button properties
    private int closeButtonShowTime;
    private String closeButtonText = "Close";
    private Color closeButtonTextColor = Color.blue;
    private Color closeButtonBackgroundColor = Color.white;
    private int closeButtonTransparency = 255;
    private Point closeButtonLocation;

    private AdServerInterstitialView adServerInterstitialView;
    private JFrame frame;
    private Dimension formDimension = new Dimension(100, 200);
    private TransparentButton closeButton;

    private Timer autoCloseInterstitialTimer;
    private Timer closeButtonShowTimer;

    public AdServerInterstitialView(String site, String zone) throws AdServerException, IOException {
        super(site, zone);
        adServerInterstitialView = this;
        setInternalBrowser(true);
    }

    // Show interstitial advertising
    public void open() throws IOException {
        javax.swing.SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                closeButton = new TransparentButton(closeButtonText);
                closeButton.setForeground(closeButtonTextColor);
                closeButton.setBackground(closeButtonBackgroundColor);
                closeButton.setTransparency(closeButtonTransparency);
                closeButton.addActionListener(new CloseButtonActionListener());

                // run gui part
                frame = new JFrame();
                frame.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
                frame.addWindowListener(adServerInterstitialView.getWindowListener());

                frame.getContentPane().add(adServerInterstitialView);

                Image icon = Toolkit.getDefaultToolkit().getImage(
                        this.getClass().getClassLoader().getResource("images/mOcean.gif"));
                frame.setIconImage(icon);

                frame.setAlwaysOnTop(true);
                frame.setSize(formDimension);
                frame.setResizable(false);
                frame.setVisible(true);

                // Get the size of the screen
                Dimension dim = Toolkit.getDefaultToolkit().getScreenSize();
                // Determine the new location of the window
                int w = frame.getSize().width;
                int h = frame.getSize().height;
                int x = (dim.width - w) / 2;
                int y = (dim.height - h) / 2;
                // Move the window
                frame.setLocation(x, y);

                frame.addWindowListener(new WindowAdapter() {
                    @Override
                    public void windowClosing(WindowEvent e) {
                        releaseResources();
                    }
                });

                if (autoCloseInterstitialTime > 0) {
                    ActionListener formHider = new ActionListener() {
                        public void actionPerformed(ActionEvent e) {
                            autoCloseInterstitialTimer.stop();
                            if (popup != null) {
                                popup.hide();
                            }
                            frame.dispose();
                            releaseResources();
                        }
                    };

                    autoCloseInterstitialTimer = new Timer(autoCloseInterstitialTime * 1000, formHider);
                    autoCloseInterstitialTimer.start();
                }

                if (closeButtonShowTime > 0) {
                    ActionListener closeButtonShower = new ActionListener() {
                        public void actionPerformed(ActionEvent e) {
                            closeButtonShowTimer.stop();
                            int x = frame.getX() + closeButtonLocation.x;
                            int y = frame.getY() + closeButtonLocation.y;
                            closeButtonLocation = new Point(x, y);
                            PopupFactory factory = PopupFactory.getSharedInstance();
                            popup = factory.getPopup(
                                    frame, closeButton, closeButtonLocation.x, closeButtonLocation.y);
                            popup.show();
                            frame.setDefaultCloseOperation(JFrame.HIDE_ON_CLOSE);
                        }
                    };

                    closeButtonShowTimer = new Timer(closeButtonShowTime * 1000, closeButtonShower);
                    closeButtonShowTimer.start();
                }
            }
        });
    }

    class CloseButtonActionListener implements ActionListener {
        public void actionPerformed(ActionEvent e) {
            releaseResources();
            if (popup != null) {
                popup.hide();
            }
            frame.dispose();
        }
    }

    // Get auto-close interstitial time.
    public Integer getAutoCloseInterstitialTime() {
        return autoCloseInterstitialTime;
    }

    // Set auto-close interstitial time.
    public void setAutoCloseInterstitialTime(Integer autoCloseInterstitialTime) {
        this.autoCloseInterstitialTime = autoCloseInterstitialTime;
    }

    // Get show close button after delay.
    public Integer getCloseButtonShowTime() {
        return closeButtonShowTime;
    }

    // Set show close button after delay.
    public void setCloseButtonShowTime(Integer closeButtonShowTime) {
        this.closeButtonShowTime = closeButtonShowTime;
    }

    // get text of close button
    public String getCloseButtonText() {
        return closeButtonText;
    }

    // set text of close button
    public void setCloseButtonText(String closeButtonText) {
        this.closeButtonText = closeButtonText;
    }

    // get text color of close button
    public Color getCloseButtonTextColor() {
        return closeButtonTextColor;
    }

    // set text color of close button
    public void setCloseButtonTextColor(Color closeButtonTextColor) {
        this.closeButtonTextColor = closeButtonTextColor;
    }

    // get background color of close button
    public Color getCloseButtonBackgroundColor() {
        return closeButtonBackgroundColor;
    }

    // set background color of close button
    public void setCloseButtonBackgroundColor(Color closeButtonBackgroundColor) {
        this.closeButtonBackgroundColor = closeButtonBackgroundColor;
    }

    // get transparency coefficient (0 - 255)
    public int getCloseButtonTransparency() {
        return closeButtonTransparency;
    }

    // set transparency coefficient (0 - 255)
    public void setCloseButtonTransparency(int closeButtonTransparency) {
        this.closeButtonTransparency = closeButtonTransparency;
    }

    // get location of close button
    public Point getCloseButtonLocation() {
        return closeButtonLocation;
    }

    // set location of close button
    public void setCloseButtonLocation(Point closeButtonLocation) {
        this.closeButtonLocation = closeButtonLocation;
    }

    // get size of popup window
    public Dimension getFormDimension() {
        return formDimension;
    }

    // set size of popup window
    public void setFormDimension(Dimension formDimension) {
        this.formDimension = formDimension;
    }

    // JButton with transparency support
    class TransparentButton extends JButton {
        private final int max_transparency = 255;
        private int transparency = 255 / 2;

        public TransparentButton(String text) {
            super(text);
            setOpaque(false);
        }

        public void paint(Graphics g) {
            Graphics2D g2 = (Graphics2D) g.create();
            g2.setComposite(AlphaComposite.getInstance(
                    AlphaComposite.SRC_OVER, 1.0f * transparency / max_transparency));
            super.paint(g2);
            g2.dispose();
        }

        public int getTransparency() {
            return transparency;
        }

        public void setTransparency(int transparency) {
            if (transparency < 0) {
                transparency = 0;
            }
            if (transparency > max_transparency) {
                transparency = max_transparency;
            }
            this.transparency = transparency;
        }
    }
}